//
//  RSStatusBottomButtonView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/12.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit

class RSStatusBottomButtonView: UIView {
    
    // 三个按钮控件
    // 转发按钮
     var retweetBtn: UIButton?
    // 评论按钮
     var commentBtn: UIButton?
    // 赞按钮
     var likeBtn: UIButton?
    
    var statusViewModel: RSStatusViewModel?{
    
        didSet{
        
            retweetBtn?.setTitle(statusViewModel?.reweetCountConten, forState: .Normal)
            commentBtn?.setTitle(statusViewModel?.commentCountConten, forState: .Normal)
            likeBtn?.setTitle(statusViewModel?.unlikeCountContent, forState: .Normal)
//            likeBtn?.setTitle("18909", forState: .Normal)
            
        }
    }
    
    
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupBottomView()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置本控件
    private func setupBottomView(){
    
        // 调用私有方法, 实现创建并添加按钮的两个步骤
        retweetBtn = addSubBtn("转发", imageName: "timeline_icon_retweet")
        commentBtn = addSubBtn("评论", imageName: "timeline_icon_comment")
        likeBtn = addSubBtn("赞", imageName: "timeline_icon_unlike")
    
    
        // 添加两条分割线
        let oneLine = addSeparateLine()
        let twoLine = addSeparateLine()
        
        // 设置约束
        retweetBtn!.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        }
        
        commentBtn!.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetBtn!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(likeBtn!)
        }
        
        likeBtn!.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentBtn!.snp_trailing)
        }
        
        oneLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetBtn!.snp_trailing)
            make.centerY.equalTo(self)
        }
        twoLine.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentBtn!.snp_trailing)
        }

    
    
    }
    
    
    
    
    /*
     定义一个 添加按钮的方法, 可以避免代码臃肿
     */
    private func addSubBtn(title: String, imageName:String) -> UIButton {
    
        let btn = UIButton()
        // 标题
        btn .setTitle(title, forState: .Normal)
        // 标题颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //  设置字体
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 设置图片
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        
        //  设置背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        // 添加到父控件中
        addSubview(btn)
        
        return btn
        
    }
    
    /*
     添加两根分割线的方法
     */
    private func addSeparateLine() -> UIImageView{
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
    

}
